﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;

namespace SimpleNotify.Client
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            if (IsProcessRunning("SimpleNotify.Client"))
            {
                MessageBox.Show("An instance of SimpleNotify is already running.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                try
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    MainComponent m = new MainComponent();
                    Application.Run();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unhandled Exception: " + Environment.NewLine + Environment.NewLine + ex.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        static bool IsProcessRunning(string process)
        {
            Process[] prozesse = System.Diagnostics.Process.GetProcessesByName(process);

            int count = 0;
            foreach (var prozess in prozesse)
            {
                if (String.Equals(prozess.MainModule.FileName, Application.ExecutablePath))
                {
                    count++;
                }
            }

            return (count > 1);
        }
    }
}
